programming4us
           
 
 
SQL Server

PowerShell in SQL Server 2008

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
7/20/2011 11:33:39 AM
This section covers what has been specifically added to SQL Server 2008 to provide support for PowerShell.

Adding PowerShell Support

Note

This discussion is based on SQL Server 2008 Enterprise Edition. SQL Server 2008 Express doesn’t provide all the same features. For example, the Express version doesn’t provide the SQL Server Agent functionality briefly discussed later.


Either during the initial installation of SQL 2008 or afterward while changing the installed features, you are able to add the SQL Server–specific PowerShell features by using the setup utility. The Management Tools-Basic feature must be added, as shown in Figure 1.

Figure 1. Installing the PowerShell features.

The Management Studio add-on is also required to get the PowerShell-specific features installed. This specific feature adds the following:

  • Management Studio— The graphical user interface for managing SQL Server 2008

  • SQLCMD— The utility that SQL scripters should already be familiar with

  • SQL Server PowerShell provider— The PowerShell-specific extra functionality

Note

An added bonus is that you can install Management Studio by itself on either the server or another remote system, and be able to administer your SQL Server database remotely. Consideration should be given to whether the SQL Server is set up for remote connections, and the appropriate firewall changes have been made to the network and on the database server, if applicable.


Accessing PowerShell

Now that you have added the SQL Server–specific PowerShell features, you can access a SQL Server PowerShell session.

Note

From this point on, we make the distinction between PowerShell and SQL Server PowerShell. The details are discussed shortly, but for now PowerShell is the basic or default PowerShell console, and SQL Server PowerShell is a more restricted version of PowerShell that has all the SQL Server–specific PowerShell features packaged within it.


SQL Server PowerShell can be accessed in either of two ways:

  • You can open SQL Server PowerShell via the SQL Server Management Studio by right-clicking on a particular object in the Object Explorer and selecting Start PowerShell, as shown in Figure 2. This way is handy because it provides a prompt in the SQL provider (which is discussed shortly) in the location of the object that was right-clicked.

    Figure 2. Accessing PowerShell via SSMS.
  • You also can open SQL Server PowerShell directly from regular DOS or a regular PowerShell console by simply navigating to the appropriate location, as shown in Figure 3.

    Figure 3. Accessing PowerShell using sqlps.exe.


SQL Server PowerShell

When you first get into SQL Server PowerShell, you might notice that this is a restricted version of the default PowerShell console. In other words, several of the core cmdlets are not available in SQL Server PowerShell, and others might not work exactly the same way. For example, invoking the Get-Command cmdlet alone with no other arguments does not list all the available commands.

Note

Running Get-Command in SQL Server PowerShell without any parameters might generate the following message:

Get-Command : Object reference not set to an instance of an object.

However, other invocations of this command work fine (such as the other examples of Get-Command in this section). Microsoft has identified this issue but as of this writing has not released a fix for it. Because the error occurs only within the SQL provider, the current workaround is to switch from the SQL Server provider to a different drive (such as C:\) before running Get-Command:

PS SQLSERVER:\> cd c:\
PS C:\> Get-Command
<output deleted>
PS C:\> cd SQLSERVER:
PS SQLSERVER:\>


SQL Provider

The term provider was briefly introduced. The SQL team decided to implement a SQL Server provider. What this provides is a layout of the SQL object structure, which resembles that of a regular file system.

You use the SQL provider when accessing SQL Server PowerShell via SQL Server Management Studio: depending on what object you right-click to access SQL Server PowerShell, a prompt opens in the context of that particular object. Basically, the way certain commands work is also affected by the current location within the SQL Server provider. Here are two different examples of being placed in different locations within the SQL Server provider. In the first example, the AdventureWorks2008R2 database was right-clicked within SSMS, as shown in Figure 4. In the second example, a specific table (Person.Address) within the AdventureWorks2008R2 database was right-clicked, as shown in Figure 5.

Figure 4. SQL Server provider at the database level.

Figure 5. SQL Server provider at the table level.

When you start the SQL Server PowerShell minishell by simply invoking sqlps.exe as seen earlier, a prompt opens at the root of the SQL Server provider.

Note

Some of the core cmdlets like Get-Item, Remove-Item, and New-Item are typically used within providers to retrieve, remove, and create items, respectively. Within the SQL Server provider, creating items using the New-Item cmdlet is currently not supported. Other methods are required to actually create items.


SQL Cmdlets

A number of cmdlets available in SQL Server PowerShell are part of the basic PowerShell functionality. However, within SQL Server PowerShell, five additional cmdlets are available only after the minishell is started (or if the snap-in is loaded manually, which is not covered in any detail here):

  • Invoke-PolicyEvaluation— A cmdlet that evaluates a SQL Server Policy-Based Management policy (or policies) against a target server.

  • Invoke-SqlCmd— A cmdlet that runs any regular T-SQL command and any languages and commands supported by the sqlcmd utility, which may be more familiar to most users.

  • Encode-SqlName— A cmdlet that helps to encode SQL Server identifiers into a format that PowerShell can use.

  • Decode-SqlName— A cmdlet that helps to return the original SQL Server identifiers from a value previously given by the Encode-SqlName cmdlet.

  • Convert-UrnToPath— A cmdlet that converts the SMO Uniform Resource Name to a SQL Server provider path.

Later, examples of using the core cmdlets are provided, as well as the first two cmdlets introduced in the preceding list.

Note

For more details on the three other cmdlets not discussed here, see the built-in help for more information and examples.


Note

The intent is to ship more cmdlets as part of SQL-PowerShell in the future after more database users become more familiar with SQL Server PowerShell.


SQL Server Agent Support

PowerShell has been integrated into the SQL Server Agent subsystem. In other words, you can create jobs that call PowerShell-specific commands to run.

Other -----------------
- Protecting SQL Server Data : SCHEMA ARCHITECTURE STRATEGIES - Using Database Object Schemas
- Protecting SQL Server Data : SCHEMA ARCHITECTURE STRATEGIES - Protection via Normalization
- Troubleshooting and Optimizing SQL Server 2005 : Server Configuration Maintenance
- Troubleshooting and Optimizing SQL Server 2005 : Tuning the Database Structure
- Troubleshooting and Optimizing SQL Server 2005 : Data Analysis and Problem Diagnosis
- SQL Injection Attacks and Defense : Exploiting the Operating System - Consolidating Access
- SQL Injection Attacks and Defense : Executing Operating System Commands
- Administering SQL Server 2008 with PowerShell : PowerShell Scripting Basics (part 2)
- Administering SQL Server 2008 with PowerShell : PowerShell Scripting Basics (part 1)
- Administering SQL Server 2008 with PowerShell : Overview of PowerShell
- SQL Server 2008 Scheduling and Notification : Scripting Jobs and Alerts, Multiserver Job Management & Event Forwarding
- SQL Server 2008 Scheduling and Notification : Managing Alerts
- SQL Injection Attacks and Defense : Accessing the File System (part 2) - Writing Files
- SQL Injection Attacks and Defense : Accessing the File System (part 1) - Reading Files
- SQL Server 2008 Scheduling and Notification : Managing Jobs
- SQL Server 2008 Scheduling and Notification : Managing Operators
- SQL Server 2008 Scheduling and Notification : Configuring the SQL Server Agent
- SQL Server 2008 : Database Mail - Related Views and Procedures
- SQL Server 2008 : Database Mail - Using SQL Server Agent Mail
- SQL Server 2008 : Sending and Receiving with Database Mail
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us